//使用与frontSideDecorator.jsp，原来的z名字是scauCmi2.js，
$(function() {
	$("div.headItemLink").mouseenter(function() {
		var aid = $(this).attr("aid");
		$("div.headMenu[aid=" + aid + "]").show();
		var left = $("div.headItemLink[aid=" + aid + "]").offset().left;
		var scroL = $(document).scrollLeft();
		$("div.headMenu[aid=" + aid + "]").css("left", left + 35 - scroL);
	});
	$("div.headItemLink").mouseleave(function() {
		var aid = $(this).attr("aid");
		$("div.headMenu[aid=" + aid + "]").hide();
	});

});

$(function() {
	$("div.scau-item-title").click(function() {
		var lid = $(this).attr("lid");
		$("div.scau-news-list[lid=" + lid + "]").show();
		var div = $("div.scau-news-list");
		for (var i = 0; i < div.length; i++) {
			if (i + 1 == lid) {
				$("div.scau-news-list[lid=" + lid + "]").show();
			} else {
				$("div.scau-news-list[lid=" + (i + 1) + "]").hide();
			}
		}
	});
});

// NewEvent
$(function() {
	$("#load-file").change(function(e) {
		var file = e.target.files[0] || e.dataTransfer.files[0];
		if (file) {
			var reader = new FileReader();
			reader.onload = function() {
				$("#eventImg").attr("src", this.result);
				$("#eventImg").css("display", "inline-block");
			}
			reader.readAsDataURL(file);
		}
		$("#scau-unload").css("display", "none");
		$("#scau-uploaded").css("display", "inline-block");

	});
	$("#reload-file").change(function(e) {
		var file = e.target.files[0] || e.dataTransfer.files[0];
		if (file) {
			var reader = new FileReader();
			reader.onload = function() {
				$("#eventImg").attr("src", this.result);
			}
			reader.readAsDataURL(file);
		}
	});
});

/*
 * $(document).scroll(function() { var scroH = $(document).scrollTop(); if
 * (scroH > 200) { $(".header").addClass("active");
 * $(".headerLogo2").css("display", ""); } else {
 * $(".header").removeClass("active"); $(".headerLogo2").css("display", "none"); }
 * });
 */

function showDetail() {
	var that = event.currentTarget;
	var brother = $(that).next();
	var children = $(that).children();
	var child = children[1];
	var display = $(brother).css("display");
	if (display == "none") {
		$(brother).css("display", "");
		$(child).attr("src", "images/xia.png");
	} else {
		$(brother).css("display", "none");
		$(child).attr("src", "images/right-ExpandDetail.png");
	}
}

function showRecordDetail() {
	var that = event.currentTarget;
	var lid = $(that).attr("lid");
	var display = $(".detail" + lid).css("display");
	if (display == "none") {
		$(".detail" + lid).css("display", "");
		$(that).attr("src", "images/下圈.png");
		var width = $(".scau-record-wrap").css("width");
		$(".scau-wrap-item").children().children().css("width",
				(parseFloat(width) - 1) / 3);
		$(".scau-wrap-item").height((parseFloat(width) - 1) / 3/1.5);
		$(".scau-wrap-box").height((parseFloat(width) - 1) / 3/1.5);
	} else {
		$(".detail" + lid).css("display", "none");
		$(that).attr("src", "images/右圈.png");
	}
}

window.onresize = function() {
	var width = $(".scau-record-wrap").css("width");
	if(width != '0px' ){
	$(".scau-wrap-item").children().children().css("width",
			(parseFloat(width) - 1) / 3);
	$(".scau-wrap-item").height((parseFloat(width) - 1) / 3/1.5);
	$(".scau-wrap-box").height((parseFloat(width) - 1) / 3/1.5);
	}
};

function showUris() {
	var that = event.currentTarget;
	var lid = $(that).attr("lid");
	var display = $(".uri" + lid).css("display");
	if (display == "none") {
		$(".uri" + lid).css("display", "");
		$(that).attr("src", "images/下圈.png");
	} else {
		$(".uri" + lid).css("display", "none");
		$(that).attr("src", "images/右圈.png");
	}
}

function showEvents() {
	var that = event.currentTarget;
	var lid = $(that).attr("lid");
	var display = $(".event" + lid).css("display");
	if (display == "none") {
		$(".event" + lid).css("display", "");
		$(that).attr("src", "images/下圈.png");
	} else {
		$(".event" + lid).css("display", "none");
		$(that).attr("src", "images/右圈.png");
	}
}

function showPictures() {
	var that = event.currentTarget;
	var lid = $(that).attr("lid");
	var display = $("div.scau-picture-content[lid=" + lid + "]").css("display");
	var brotherContent = $("div.scau-picture-content");
	var brotherImg = $(".scau-picture-img");
	for (var i = 0; i < brotherContent.length; i++) {
		$(brotherContent[i]).css("display", "none");
		$(brotherImg[i]).attr("src", "images/add.png");
	}
	if (display == "none") {
		$("div.scau-picture-content[lid=" + lid + "]").css("display", "");
		$(that).attr("src", "images/delete.png");
	} else {
		$("div.scau-picture-content[lid=" + lid + "]").css("display", "none");
		$(that).attr("src", "images/add.png");
	}
}

// Carousel
var recordlen = $(".scau-record-content").attr("recordlen");
var currentcarousel = [];
for(var i=0; i<recordlen; i++){
	var detaillen = $(".detail"+(i+1)).attr("detaillen");
	var detailary = [];
	for(var j=0; j<detaillen; j++){
		detailary[j]=0;
	}
	currentcarousel[i] = detailary;
}

function prev_button(){
	var that = event.currentTarget;
	var i = $(that).attr("iid") -1;
	var j = $(that).attr("jid") -1;
	var next = $(that).next();
	var children = $(that).parent().children().children().children();
	var child2 = $(that).parent().children().children();
	var wrap = child2[0];
	var len = $(wrap).attr("len") - 1;
	if(currentcarousel[i][j] == 0){
		$(that).css("cursor","not-allowed");
	} else {
		$(next).css("cursor","pointer");
		$(children[--currentcarousel[i][j]]).css("display", "inline-block");
	} 
}

function next_button(){
	var that = event.currentTarget;
	var i = $(that).attr("iid") -1;
	var j = $(that).attr("jid") -1;
	var pre = $(that).prev();
	var children = $(that).parent().children().children().children();
	var child2 = $(that).parent().children().children();
	var wrap = child2[0];
	var len = $(wrap).attr("len") - 1;
	if(currentcarousel[i][j] + 2 == len){
		$(that).css("cursor","not-allowed");
	} else {
		$(pre).css("cursor","pointer");
		$(children[currentcarousel[i][j]]).css("display", "none");
		var cnt = currentcarousel[i][j];
		for(cnt++; cnt <= currentcarousel[i][j]+3; cnt++){
			$(children[cnt]).css("display", "inline-block");
		}
		currentcarousel[i][j]++;
	} 
}

function addNewEvent() {
	$(".scau-success").css("display", "");
}
function backEvent() {
	$(".scau-swal-bg").css("display", "none");
}
function errorEvent() {
	$(".scau-error").css("display", "");
}

function check() {
	var selectEvent = $("#selectEvent").val();
	var eventName = $("#eventName").val();
	var eventDescription = $("#eventDescription").val();
	var selectAddress = $("#selectAddress").val();
	var province = $("#province").val();
	var city = $("#city").val();
	var area = $("#area").val();
	var street = $("#street").val();
	var selectSubject = $("#selectSubject").val();
	var subject = $("#subject").val();
	var selectFood = $("#selectFood").val();
	var food = $("#food").val();
	var selectProducer = $("#selectProducer").val();
	var selectFoodtype = $("#selectFoodtype").val();
	var selectBrand = $("#selectBrand").val();
	
	var click = 1;
	var error = "请填写以下信息：";
	if (selectEvent == "") {
		error += "\n关联事件";
		click = 0;
	}else{
		if(selectEvent == -2){// 新的父事件
			if (selectAddress == ""
				&& (province == "" || city == "" || area == "" || street == "" || address == "")) {
			error += " \n地址";
			click = 0;
		}
		if (selectSubject == "" && subject == "") {
			error += " \n主体";
			click = 0;
		}
		if (selectFood == "" && food == "") {
			error += " \n食物";
			click = 0;
		}
		}else{
			click =1;
		}			
	}
	if (eventName == "") {
		error += " \n名称";
		click = 0;
	}
	if (eventDescription == "") {
		error += " \n描述";
		click = 0;
	}
	if (click == 0) {
		$("#scau-error-info").text(error);
		errorEvent();
	} else {
		addNewEvent();
		$("#submit").click();
	}
}